package $NAMESPACE.domain.service.impl;

import com.ugeez.commons.mybatisplus.service.impl.AbstractServiceImpl;
import $NAMESPACE.domain.service.UserRoleService;
import $NAMESPACE.persistence.dao.UserRoleMapper;
import $NAMESPACE.persistence.entity.AppUser;
import $NAMESPACE.persistence.entity.UserRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleServiceImpl extends AbstractServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Override
    public List<UserRole> getByUserId(Long userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public void addRole(Long userId, String role) {
        try {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRole(role);
            this.save(userRole);
        } catch (DuplicateKeyException e) {
            //ignore
        }
    }

    @Override
    public void removeRole(Long userId, String role) {
        QueryWrapper<UserRole> filter = new QueryWrapper<>();
        filter.eq("user_id", userId);
        filter.eq("role", role);
        this.remove(filter);
    }
}
